//
//  RentHouseModel.m
//  Liuhe
//
//  Created by zr on 2018/8/20.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "RentHouseModel.h"

@implementation RentHouseModel

+(instancetype)modelWithDictionary:(NSDictionary *)dic{
    
    RentHouseModel *model =[[RentHouseModel alloc] init];
    
    [model setValuesForKeysWithDictionary:dic];
    
    return model;
}


+(void)rentModelWithDict:(NSDictionary *)dict WithSucess:(void (^)(NSArray<RentHouseModel *> *))sucessBlock error:(void (^)(void))errorBlock{
    
    //    NSDictionary * parameters;
    NSString * url;
    if (dict) {
        
        NSString * keyType;
        NSString * valueType;
        
        NSArray * allkeys = [dict allKeys];
        //    NSLog(@"allkeys %@",allkeys);
        
        for (int i = 0; i < allkeys.count; i++)
        {
            keyType = [allkeys objectAtIndex:i];
            valueType  = [dict objectForKey:keyType];
        }
        
        if ([valueType isEqualToString:@"不限"]) {
            
            url = @"/index/Liuheonline/leaseList";
        }else{
           
            NSString * type;
            
            if ([keyType isEqualToString:@"one"]) {
                
                type = @"region";
            }else if ([keyType isEqualToString:@"two"]) {
                
                type = @"price";
            }else if ([keyType isEqualToString:@"three"]) {
                
                type = @"room";
            }
            
            url = [NSString stringWithFormat:@"/index/Liuheonline/leaseList?%@=%@",type,valueType];
        }
    
    }else{
        
        url = @"/index/Liuheonline/leaseList";
    }
    
    NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
    NSString *encodeUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
    
    [[NetworkTool sharedTools] GET:encodeUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        //        NSLog(@"====%@",responseObject);
        
        NSString * msg = responseObject[@"msg"];
        
        if ([msg isEqualToString:@"无数据"]) {
            
            NSArray * array = nil;
            sucessBlock(array);
            
        }else{
            
            NSArray * paramArray = responseObject[@"data"];
            
            //            NSLog(@"==%@",paramArray);
            
            NSMutableArray *mArray = [NSMutableArray array];
            
            [paramArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                RentHouseModel * model = [RentHouseModel modelWithDictionary:obj];
                
                [mArray addObject:model];
                
            }];
            
            if(sucessBlock){
                sucessBlock(mArray.copy);
            }
        }
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSLog(@"errror===%@",error);
    }];
    
}


- (void)setValue:(id)value forKey:(NSString *)key{
    
    if ([key isEqualToString:@"id"]) {
        
        self.ID = value;
    }else{
        
        [super setValue:value forKey:key];
    }
    
}


-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}



@end
